package com.servlets.user;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.GlobalConstants;
import com.core.protocols.IServiceLayerDelegate;
import com.managers.session.SessionManager;
import com.servicelayer.user.UserListLayer;
import com.servlets.TaskTrackerServlet;

/**
 * Servlet implementation class ProjectDelete
 */

public class UserResetPassword
        extends TaskTrackerServlet
        implements IServiceLayerDelegate {

    private String              redirection  = null;
    private HttpServletResponse httpResponse = null;



    @Override
    public <T> void responseResponseItemsList ( final List <T> items,
                                                final Object request,
                                                final Exception exception ) {

        if ( httpResponse == null ) {
            return;
        }

        if ( exception != null ) {

        }

        if ( redirection == null ) {
            return;
        }

        try {
            httpResponse.sendRedirect(redirection);
        } catch ( IOException e ) {
            e.printStackTrace();
        }

    }

    private static final long serialVersionUID = 1L;



    @Override
    protected void doPost ( HttpServletRequest request,
                            HttpServletResponse response )
            throws ServletException, IOException {

        this.httpResponse = response;

        String newPassword = request.getParameter("newPassword");
        Integer userId = null;
        try {
            userId = Integer.parseInt(request.getParameter(GlobalConstants.SERVLET_PARAM_KEY));
        } catch ( NumberFormatException e ) {
            userId = null;
        }

        UserListLayer userListLayer = new UserListLayer();
        userListLayer.setLayerDelegate(this);
        userListLayer.resetPassword(userId, newPassword, this);

    }



    @Override
    protected void doGet ( HttpServletRequest request,
                           HttpServletResponse response )
            throws ServletException, IOException {


        redirection = SessionManager.sharedInstance().lastExecutedURL();
        super.doGet(request, response);
    }

}
